package com.marisfrolg.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.database.Cursor;
import android.util.SparseArray;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.marisfrolg.entity.json.InbItem;
import com.marisfrolg.entity.json.InbItemSub;

public class InbItemSubControl {
	private DbUtils db;
	public InbItemSubControl(DbUtils db){
		this.db=db;
	}
	public void spilt(int inbid,int preNum,String autoBin){
		List<InbItemSub> list=getList(inbid);
		InbItemSub iisSrc=list.get(0);
		InbItemSub iis=new InbItemSub();
		iis.setInbId(iisSrc.getInbId());
		iis.DOCID=iisSrc.DOCID;
		iis.ITEMID=iisSrc.ITEMID;
		iis.PRODUCTID=iisSrc.PRODUCTID;
		iis.PRODUCTNO=iisSrc.PRODUCTNO;
		iis.setSTOTYPE(iisSrc.getSTOTYPE());
		iis.setTotal(iisSrc.getTotal());
		iis.QTY=preNum;
		iis.SPLIT="X";
		iisSrc.SPLIT="X";
		iis.STOBIN=autoBin;
		iis.UOM=iisSrc.UOM;
		iis.TAPOS=list.size()+1;
		iis.setLTYPT(iisSrc.getLTYPT());
		
		try {
			db.saveBindingId(iis);
			db.update(iisSrc, "split");
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	public void delete(int id){
		try {
			db.deleteById(InbItemSub.class, id);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	
	public List<InbItemSub> getAllList(){
		List<InbItemSub> list=null;
		try {
			list = db.findAll(Selector.from(InbItemSub.class));
		} catch (DbException e) {
			e.printStackTrace();
		}
		if(list==null){
			list=new ArrayList<InbItemSub>();
		}
		return list;
	}
	
	
	
	public List<InbItemSub> getList(int inbid){
		List<InbItemSub> list=null;
		try {
			list = db.findAll(Selector.from(InbItemSub.class).where("inbid", "=", inbid));
		} catch (DbException e) {
			e.printStackTrace();
		}
		if(list==null){
			list=new ArrayList<InbItemSub>();
		}
		return list;
	}
	/**
	 * @return the sum
	 */
	public int getSumByInbid(int inbid) {
		int sum=0;
		for (InbItemSub ib : getList(inbid)) {
			sum+=ib.QTY;
		}
		return sum;
	}
	public int getSum() {
		int sum=0;
		for (InbItemSub ib : getAllList()) {
			sum+=ib.QTY;
		}
		return sum;
	}
	public int getSurplus(){
		return getTotalSum()-getConfirmSum()>0?getTotalSum()-getConfirmSum():0;
	}
	public int getSurplusByInbid(int inbid){
		return getTotalSumByInbid(inbid)-getConfirmSumByInbid(inbid)>0?getTotalSumByInbid(inbid)-getConfirmSumByInbid(inbid):0;
	}
	public SparseArray<String> getCutMap(){
		SparseArray<String> map=new SparseArray<String>();
		try {
			Cursor c=db.execQuery("select a.id,a.qty-b.sm sm from inbitem a left join (select inbid,sum(qty) sm from inbitemsub group by inbid) b on a.id=b.inbid where a.id in (select inbid from inbitemsub)");
			c.moveToFirst();
			for(int i=0;i<c.getCount();i++){
				System.out.println(c.getString(0)+"====="+c.getShort(1));
				int id=Integer.parseInt(c.getString(0));
				InbItem ii=db.findById(InbItem.class, id);
				int cut=Integer.parseInt(c.getString(1));
				map.put(cut, ii.getPRODUCTNO());
				c.moveToNext();
			}
		} catch (DbException e) {
			e.printStackTrace();
		}
		return map;
	}
	public int getCutByInbid(int inbid){
		int cut=-999;
		try {
			Cursor c=db.execQuery("select a.qty-b.sm from inbitem a left join (select inbid,sum(qty) sm from inbitemsub group by inbid) b on a.id=b.inbid where a.id="+inbid);
			c.moveToFirst();
			cut=Integer.parseInt(c.getString(0));
		} catch (DbException e) {
			e.printStackTrace();
		}
		return cut;
	}
	public int getConfirmSum() {
		int sum=0;
		for (InbItemSub ib : getAllList()) {
			if(ib.isCheckQTY()){
				sum+=ib.QTY;
			}
		}
		return sum;
	}
	public int getConfirmSumByInbid(int inbid) {
		int sum=0;
		for (InbItemSub ib : getList(inbid)) {
			if(ib.isCheckQTY()){
				sum+=ib.QTY;
			}
		}
		return sum;
	}
	public int getTotalSumByInbid(int inbid) {
		int sum=0;
		try {
			Cursor c=db.execQuery("select qty from inbitem where id="+inbid);
			c.moveToFirst();
			sum=Integer.parseInt(c.getString(0));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sum;
	}
	public int getTotalSum() {
		int sum=0;
		try {
			Cursor c=db.execQuery("select sum(qty) sm from inbitem where co != 'X'");
			c.moveToFirst();
			sum=Integer.parseInt(c.getString(0));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sum;
	}
	public int getPickTotalSum() {
		int sum=0;
		try {
			Cursor c=db.execQuery("select sum(qty) sm from inbitem where id in (select inbid from inbitemsub)");
			c.moveToFirst();
			sum=Integer.parseInt(c.getString(0));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sum;
	}
	public boolean isAllCheck() {
		boolean isOK=true;
		List<InbItemSub> list=getAllList();
		if(list==null||list.size()==0){
			return false;
		}
		for (InbItemSub ib : list) {
			isOK=isOK&&ib.isCheckQTY();
		}
		return isOK;
	}
}
